<template>
  <view v-if="shenheStatus == 0" class="page-container">
    <view class="page-content">
      <!-- 商户地址信息 -->
      <view class="shop-address">
        <view class="shop-info" @click="guideToShop">
          <view class="flex-full">
            <view class="name">{{ store.name }}</view>
            <view class="address">
              <image :src="asyncImgs.alipay.local_icon" mode="" />
              <text>{{ store.address }}</text>
            </view>
          </view>
          <view class="shu-line"></view>
        </view>
        <image
          class="shop-phone-icon"
          :src="asyncImgs.alipay.green_phone_icon"
          @click="phoneToShop(store.phone)"
          mode=""
        />
      </view>
      <!-- 就餐码 -->
      <view class="code-section">
        <view class="code-time">用餐时间：{{ appointData.order_time }}</view>
        <view class="code-num" v-if="!isOffTime">{{
          appointData.order_code
        }}</view>
        <view class="off-time" v-else>已过预约时间</view>
        <view class="code-title">就餐码</view>
      </view>
      <!-- 凭证码说明 -->
      <view class="code-summary">
        <view class="summary-title">凭证码说明</view>
        <view>{{ code_explain || "" }}</view>
      </view>
      <!-- 取消预约按钮 -->
      <view class="cancle-section">
        <view class="cancle-btn" @click="cancleAppoint">{{
          isOffTime ? "重新预约" : "取消预约"
        }}</view>
      </view>
    </view>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from "@/components/shenhe/shenhe";
export default {
  components: { Shenhe },
  data() {
    return {
      store: {},
      code_explain: "暂无",
      appointData: {},
      isOffTime: false // 是否过期
    };
  },
  filters: {},
  onLoad(t) {
    if (t?.data) {
      this.appointData = JSON.parse(t.data);
      this.checkHasOffTime();
      this.store = this.appointData.store;
      this.code_explain = this.appointData.description;
    }
  },
  onShow() {},
  methods: {
    // 校验是否已过期
    checkHasOffTime() {
      let order_time = this.appointData.order_time;
      let [fTime, aTime] = order_time.split(" ");
      let [sTime, eTime] = aTime.split("-");
      let endTime = fTime + " " + eTime;
      let nowStamp = new Date().getTime();
      let endStamp = new Date(endTime).getTime();
      if (nowStamp - endStamp > 0) {
        this.isOffTime = true;
      }
    },
    // 拨打商家电话
    phoneToShop(num) {
      this.$utils.makePhoneCall(num);
    },
    // 导航到店
    guideToShop() {
      uni.openLocation({
        latitude: parseFloat(this.store.latitude),
        longitude: parseFloat(this.store.longitude)
      });
    },
    // 取消预约
    cancleAppoint() {
      if (this.isOffTime) {
        this.requestToCancle(() => {
          this.$utils.goBack();
        });
      } else {
        uni.showModal({
          title: "提示",
          content: "确认取消预约？",
          success: (res) => {
            if (res.confirm) {
              this.requestToCancle(() => {
                uni.showToast({
                  title: "取消预约成功！",
                  duration: 2000
                });
                setTimeout(() => {
                  this.$utils.goBack();
                }, 1500);
              });
            } else if (res.cancel) {
            }
          }
        });
      }
    },
    // 取消请求
    requestToCancle(cb) {
      this.$allrequest.alipay
        .cancleAppoint({ id: this.appointData.id })
        .then((res) => {
          if (!res.code) {
            cb && cb();
          }
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.page-container {
  width: 100%;
  min-height: 100vh;
  background: #1577fc;
  font-family: PingFang SC;
  padding-top: 5.07vw;

  .page-content {
    width: 93.6vw;
    background: #ffffff;
    border-radius: 2.13vw;
    margin: 0 auto;
    padding: 5.33vw;
    .shop-address {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-bottom: 5.47vw;
      border-bottom: 0.27vw dashed #e5e5e5;
      .shop-info {
        flex: 1;
        display: flex;
        align-items: center;
        color: #191919;
        padding-right: 5%;
        .flex-full {
          flex: 1;
        }
        .name {
          max-width: 71.07vw;
          font-size: 5.33vw;
          font-weight: 600;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .address {
          display: flex;
          align-items: center;
          font-size: 3.2vw;
          font-weight: 400;
          margin-top: 1.07vw;
          image {
            width: 2.74vw;
            height: 3.2vw;
            display: block;
            margin-right: 0.53vw;
            background-size: 100%;
          }
          text {
            display: block;
            max-width: 67vw;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
        }
        .shu-line {
          width: 0.13vw;
          height: 9.33vw;
          background: #e5e5e5;
          margin-left: 1.87vw;
        }
      }
      .shop-phone-icon {
        width: 5.33vw;
        height: 5.33vw;
        background-size: 100%;
      }
    }

    .code-section {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 4.53vw;
      font-weight: 400;
      color: #999999;
      padding: 20.8vw 0 19.73vw 0;
      border-bottom: 0.27vw dashed #e5e5e5;
      .code-time {
        font-size: 3.73vw;
        line-height: 4.27vw;
      }
      .code-num {
        font-size: 10.67vw;
        font-weight: 600;
        color: #191919;
        margin-top: 2.4vw;
        margin-bottom: 1.33vw;
      }
      .off-time {
        font-size: 10.67vw;
        font-weight: 600;
        color: #191919;
        margin-top: 2.4vw;
        margin-bottom: 1.33vw;
      }
    }

    .code-summary {
      margin-top: 6.67vw;
      font-size: 3.2vw;
      font-weight: 400;
      line-height: 5.33vw;
      color: #666666;
      .summary-title {
        font-size: 3.73vw;
        font-weight: 600;
        color: #191919;
        margin-bottom: 2.13vw;
      }
    }

    .cancle-section {
      border-top: 0.27vw dashed #e5e5e5;
      padding-top: 6.67vw;
      margin-top: 6.8vw;
      .cancle-btn {
        width: 79.47vw;
        height: 10.67vw;
        background: #ffffff;
        border: 0.27px solid #d7d7d7;
        border-radius: 8vw;
        line-height: 10.67vw;
        text-align: center;
        font-size: 4.27vw;
        color: #191919;
      }
    }
  }
}
</style>
